﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace HandRise.MBHOP.Controls
{
    public class ImageButton : Control
    {
        Image upImage = null;
        /// <summary>
        /// 弹起的图片

        /// </summary>
        public Image UpImage
        {
            set
            {
                upImage = value;
            }
            get
            {
                return upImage;
            }
        }

        Image downImage = null;
        /// <summary>
        /// 按下的图片

        /// </summary>
        public Image DownImage
        {
            set
            {
                downImage = value;
            }
            get
            {
                return downImage;
            }
        }

        Image disableImage = null;
        /// <summary>
        /// 禁用的图片

        /// </summary>
        public Image DisableImage
        {
            set
            {
                disableImage = value;
            }
            get
            {
                return disableImage;
            }
        }

        Font font = new Font("雅黑", 9, FontStyle.Regular);
        /// <summary>
        /// 文本的字体

        /// </summary>
        public override Font Font
        {
            get
            {
                return font;
            }
            set
            {
                font = value;
            }
        }

        Color upForeColor = Color.Black;
        /// <summary>
        /// 弹起时的文本颜色
        /// </summary>
        public Color UpForeColor
        {
            set
            {
                upForeColor = value;
            }
            get
            {
                return upForeColor;
            }
        }

        Color downForeColor = Color.White;
        /// <summary>
        /// 按下时的文本颜色
        /// </summary>
        public Color DownForeColor
        {
            set
            {
                downForeColor = value;
            }
            get
            {
                return downForeColor;
            }
        }

        Point textLocation = new Point(0, 0);
        /// <summary>
        /// 文本的位置

        /// </summary>
        Point TextLocation
        {
            get
            {
                return textLocation;
            }
            set
            {
                textLocation = value;
            }
        }


        internal ButtonStateEnum buttonState = ButtonStateEnum.Up;
        /// <summary>
        /// 图片的按下和弹起状态

        /// </summary>
        internal ButtonStateEnum ButtonState
        {
            get
            {
                return buttonState;
            }
            set
            {
                buttonState = value;
            }
        }

        private bool border = false;
        /// <summary>
        /// 是否有边框

        /// </summary>
        [DefaultValue(false)]
        public bool Border
        {
            get
            {
                return border;
            }
            set
            {
                border = value;
            }
        }


  public new bool Enabled
  {
    get { return base.Enabled; }
    set { base.Enabled = value; this.Refresh(); }
  }

 


        internal event AfterButtonPaint OnAfterButtonPaint;

        public ImageButton()
        {
            this.MouseDown += new MouseEventHandler(TSButton_MouseDown);
            this.MouseUp += new MouseEventHandler(ImageButton_MouseUp);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);

            //画背景
            if (this.Enabled)
            {
                if (this.ButtonState == ButtonStateEnum.Up)
                {
                    if (UpImage != null)
                        e.Graphics.DrawImage(UpImage, new Rectangle(0, 0, this.Width, this.Height), new Rectangle(0, 0, UpImage.Width, UpImage.Height), GraphicsUnit.Pixel);
                    else
                        this.BackColor = Color.LightGray;
                }
                else
                {
                    if (downImage != null)
                        e.Graphics.DrawImage(DownImage, new Rectangle(0, 0, this.Width, this.Height), new Rectangle(0, 0, DownImage.Width, DownImage.Height), GraphicsUnit.Pixel);
                    else
                        this.BackColor = Color.LightBlue;
                }
            }
            else
            {
                if (DisableImage != null)
                    e.Graphics.DrawImage(DisableImage, new Rectangle(0, 0, this.Width, this.Height), new Rectangle(0, 0, UpImage.Width, UpImage.Height), GraphicsUnit.Pixel);
                else
                    this.BackColor = Color.Gray;
         
            }

            //画黑边

            if (border)
            {
                e.Graphics.DrawLine(new Pen(Color.Black, 1), 0, 0, 0, this.Height);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), 0, this.Height - 1, this.Width, this.Height - 1);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), this.Width - 1, this.Height, this.Width - 1, 0);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), this.Width, 0, 0, 0);
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            //画字符串
            if (this.Text != null)
            {
                SizeF textSize = e.Graphics.MeasureString(this.Text, this.Font);

                if (textSize.Height > this.Height)
                {
                    textLocation.Y = 0;
                }
                else
                {
                    textLocation.Y = (this.Height - (int)textSize.Height) / 2;
                }
                if (textSize.Width > this.Width)
                {
                    textLocation.X = 0;
                }
                else
                {
                    textLocation.X = (this.Width - (int)textSize.Width) / 2;
                }

                if (this.ButtonState == ButtonStateEnum.Up)
                    e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.UpForeColor), TextLocation.X, TextLocation.Y);
                else
                    e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.DownForeColor), TextLocation.X, TextLocation.Y);
            }

            if (OnAfterButtonPaint != null)
                OnAfterButtonPaint(e.Graphics, this.TabIndex);
        }

        void TSButton_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.ButtonState == ButtonStateEnum.Up)
            {
                this.ButtonState = ButtonStateEnum.Down;
                this.Refresh();
            }

            //if (this.Parent != null)
            //    LoopControl(this.Parent);
        }

        void ImageButton_MouseUp(object sender, MouseEventArgs e)
        {
            if (this.ButtonState == ButtonStateEnum.Down)
            {
                this.ButtonState = ButtonStateEnum.Up;
                this.Refresh();
            }
        }

        //protected override void OnGotFocus(EventArgs e)
        //{
        //    base.OnGotFocus(e);

        //    if (this.ButtonState == ButtonStateEnum.Up)
        //    {
        //        this.ButtonState = ButtonStateEnum.Down;
        //        this.Refresh();
        //    }
        //}

        //protected override void OnLostFocus(EventArgs e)
        //{
        //    base.OnLostFocus(e);

        //    if (this.ButtonState == ButtonStateEnum.Down)
        //    {
        //        this.ButtonState = ButtonStateEnum.Up;
        //        this.Refresh();
        //    }
        //}


        private void LoopControl(Control rootControl)
        {
            ImageButton tsBtn = new ImageButton();
            foreach (Control crl in rootControl.Controls)
            {
                if (crl.GetType().Name == this.GetType().Name)
                {
                    tsBtn = (ImageButton)crl;
                    if (tsBtn.Name != this.Name && tsBtn.ButtonState == ButtonStateEnum.Down)
                    {
                        tsBtn.ButtonState = ButtonStateEnum.Up;
                        tsBtn.Refresh();
                    }
                }
                //LoopControl(crl);
            }
        }
    }
}
